// pages/mine/mine.js
const app = getApp();
var request = require("../../utils/request.js");
var wx_alert = require("../../utils/wx_alert.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show_have: true,
    phone_state:  "true"  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    //快捷导航跳转
    this.turn = request.turn;
    this.close_modal = request.close_modal;
    if (wx.getStorageSync("totalInfo").mobile || wx.getStorageSync("mobile")){
      this.setData({
        phone_state:false
      })
    }
    this.setData({
      userInfo: app.globalData.userInfo
    })

    request.request(app.globalData.getUserInfo,{},function(res){
      if(res.code == 0){
        console.log(res)
        that.setData({
          user: res.user
        })
      }
    })

    wx.getLocation({
      type: 'wgs84',
      success(res) {
        console.log(res)
        var lat = res.latitude;
        var lit = res.longitude;
        wx.setStorageSync('lat', lat);
        wx.setStorageSync('lit', lit);
      }
    })    
  },
  getPhoneNumber: function (e) {
    
    var that = this;
    if (e.detail.errMsg == "getPhoneNumber:ok"){
      var data = {
        sessionId: wx.getStorageSync('sessionId'),
        encryptedData: e.detail.encryptedData,
        iv: e.detail.iv
      }
      request.request(app.globalData.proBindMobile, data,function(res){
          console.log(res)
          if(res.code==0){
            that.setData({
              phone_state: false,
              "user.mobile": res.phone
            })
          }else{
            wx_alert.alert("授权失败，请先登录",function(res){
              wx.reLaunch({
                url: '/pages/index/index',
              })
            })
          }
      })

    }else{
      wx_alert.mag("您拒绝了小程序的授权");
      this.setData({
        phone_state:false
      })
    }

  }, 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  scane:function(e){
    var phone = e.currentTarget.dataset.phone;
    wx.navigateTo({
      url: '/pages/mine_center/mine_QR_code/index1?phone=' + phone,
    })
  },
  order_list:function(e){
    var type = e.currentTarget.dataset.type;
    wx.navigateTo({
      url: '/pages/mine_center/order_lists/order_lists?type=' + type,
    })
  },
  mine_tab:function(e){
    var url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: url,
    })
  }
})